package com.it.admin.service.Impl;

import com.it.admin.mapper.AdminMapper;
import com.it.admin.service.AdminService;
import com.it.common.entity.Business;
import com.it.common.entity.Food;
import com.it.common.entity.PaginatedResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public PaginatedResult<Business> getAllBusiness(Integer pageSize, Integer pageNum, String keywords) {
        PaginatedResult<Business> result = new PaginatedResult<>();
        if (pageSize <= 0 || pageNum <= 0) {
            result.setMessage("分页参数出错！");
            result.setTotalCount(0);
            result.setList(new ArrayList<>());
        }else {
            Integer offset = (pageNum - 1) * pageSize;
            keywords="%"+keywords+"%";
            List<Business> businesses = adminMapper.getAllBusiness(pageSize, offset, keywords);

            Integer totalCount = adminMapper.getBusinessTotalCount(keywords);

            result.setList(businesses);
            result.setTotalCount(totalCount);
            result.setMessage("请求成功！");

        }

        return result;
    }

    @Override
    public Business getBusinessById(Integer businessId) {
        return adminMapper.getBusinessById(businessId);
    }

    @Override
    public Integer createBusiness(Business business) {
        return adminMapper.insertBusiness(business);
    }

    @Override
    public Integer createFood(Food food) {
        return adminMapper.insertFood(food);
    }

    @Override
    public Integer deleteBusiness(Business business) {
        return adminMapper.deleteBusiness(business);
    }

    @Override
    public Integer deleteFood(Food food) {
        return adminMapper.deleteFood(food);
    }

    @Override
    public Integer updateBusinessInfo(Business business) {
        return adminMapper.updateBusinessInfo(business);
    }

    @Override
    public PaginatedResult<Food> getFoodListById(Integer pageSize, Integer pageNum, Integer businessId) {
        PaginatedResult<Food> result = new PaginatedResult<>();
        result.setTotalCount(0);
        result.setList(new ArrayList<>());
        if (pageSize <= 0 || pageNum <= 0) {
            result.setMessage("分页参数出错！");

        }else {
            Integer offset = (pageNum - 1) * pageSize;
            Integer totalCount = adminMapper.getFoodTotalCount(businessId);

            if(totalCount==0){
                result.setMessage("商家id不存在！");
            }else{
                List<Food> foods = adminMapper.getFoodListById(pageSize, offset, businessId);

                result.setMessage("请求成功！");

                result.setList(foods);
                result.setTotalCount(totalCount);
            }

        }



        return result;
    }

    @Override
    public Integer updateFoodInfo(Food food) {
        return adminMapper.updateFoodInfo(food);
    }
}
